<template>
  <div class="input-form-container">
    <div class="form-card">
      <h3 class="form-title">文本查询</h3>
      <div class="form-group">
        <label for="userQuery">请输入您的问题：</label>
        <textarea 
          id="userQuery" 
          v-model="query" 
          placeholder="例如：什么是多智能体系统？"
          rows="4"
          maxlength="1000"
          class="form-input"
        ></textarea>
      </div>
      
      <div class="form-options">
        <div class="option-group">
          <label for="queryType">查询类型：</label>
          <select id="queryType" v-model="queryType" class="form-select">
            <option value="text">文本问答</option>
            <option value="doc">文档查询</option>
          </select>
        </div>
        
        <div class="option-group">
          <label for="topK">返回结果数：</label>
          <select id="topK" v-model="topK" class="form-select">
            <option :value="1">1</option>
            <option :value="3">3</option>
            <option :value="5">5</option>
          </select>
        </div>
      </div>
      
      <div class="form-actions">
        <button 
          @click="submitQuery" 
          :disabled="isLoading || !query.trim()"
          class="btn-primary"
        >
          {{ isLoading ? '处理中...' : '提交查询' }}
        </button>
        <button @click="clearQuery" class="btn-secondary">
          清空
        </button>
      </div>
    </div>
    
    <!-- 结果展示区域 -->
    <div v-if="showResult" class="result-card">
      <h3 class="result-title">查询结果</h3>
      <div v-if="isLoading" class="loading-state">
        <div class="loading-spinner"></div>
        <p>正在处理您的请求...</p>
      </div>
      <div v-else-if="error" class="error-state">
        <p class="error-message">{{ error }}</p>
      </div>
      <div v-else-if="result" class="result-content">
        <div class="result-text" v-html="formattedResult"></div>
        <div v-if="result.sources" class="result-sources">
          <h4>参考来源：</h4>
          <ul>
            <li v-for="(source, index) in result.sources" :key="index">
              {{ source }}
            </li>
          </ul>
        </div>
      </div>
      <div class="result-actions">
        <button @click="showResult = false" class="btn-secondary">
          关闭结果
        </button>
        <button @click="copyResult" class="btn-secondary">
          复制结果
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { api } from '../api/request';

// 响应式数据
const query = ref('');
const queryType = ref('text');
const topK = ref(3);
const isLoading = ref(false);
const result = ref(null);
const error = ref(null);
const showResult = ref(false);

// 格式化结果显示
const formattedResult = computed(() => {
  if (!result.value?.content) return '';
  return result.value.content.replace(/\n/g, '<br>');
});

// 提交查询
const submitQuery = async () => {
  if (!query.value.trim()) return;
  
  isLoading.value = true;
  error.value = null;
  showResult.value = true;
  
  try {
    // 准备请求数据
    const requestData = {
      query_type: queryType.value,
      query: query.value.trim(),
      top_k: parseInt(topK.value)
    };
    
    // 调用API
    console.log('发送请求:', requestData);
    const response = await api.search(requestData);
    console.log('收到响应:', response);
    
    result.value = response;
  } catch (err) {
    console.error('API请求失败:', err);
    error.value = err.response?.data?.detail || '请求失败，请稍后重试';
  } finally {
    isLoading.value = false;
  }
};

// 清空查询
const clearQuery = () => {
  query.value = '';
  error.value = null;
  result.value = null;
  showResult.value = false;
};

// 复制结果
const copyResult = async () => {
  if (!result.value?.content) return;
  
  try {
    await navigator.clipboard.writeText(result.value.content);
    alert('结果已复制到剪贴板');
  } catch (err) {
    console.error('复制失败:', err);
  }
};
</script>

<style scoped>
.input-form-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
  margin: 20px 0;
}

.form-card,
.result-card {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.form-title,
.result-title {
  margin: 0 0 20px 0;
  font-size: 1.2rem;
  font-weight: 600;
  color: #fff;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.9);
}

.form-input {
  width: 100%;
  padding: 12px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  color: #fff;
  font-size: 16px;
  resize: vertical;
  min-height: 100px;
  transition: border-color 0.3s ease;
}

.form-input:focus {
  outline: none;
  border-color: #6366f1;
  background: rgba(255, 255, 255, 0.1);
}

.form-options {
  display: flex;
  gap: 20px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.option-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.option-group label {
  color: rgba(255, 255, 255, 0.9);
  font-size: 14px;
}

.form-select {
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 6px;
  color: #fff;
  cursor: pointer;
  transition: border-color 0.3s ease;
}

.form-select:focus {
  outline: none;
  border-color: #6366f1;
}

.form-actions,
.result-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  margin-top: 20px;
}

.btn-primary,
.btn-secondary {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn-primary {
  background: #6366f1;
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background: #4f46e5;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}

.btn-primary:disabled {
  background: #4b5563;
  cursor: not-allowed;
  opacity: 0.6;
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-1px);
}

.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 0;
  color: rgba(255, 255, 255, 0.8);
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(255, 255, 255, 0.1);
  border-top-color: #6366f1;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.error-state {
  padding: 20px;
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.3);
  border-radius: 8px;
}

.error-message {
  color: #ef4444;
  margin: 0;
}

.result-content {
  color: rgba(255, 255, 255, 0.9);
  line-height: 1.6;
}

.result-text {
  margin-bottom: 20px;
}

.result-sources {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.result-sources h4 {
  margin: 0 0 10px 0;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.7);
}

.result-sources ul {
  margin: 0;
  padding-left: 20px;
  color: rgba(255, 255, 255, 0.6);
  font-size: 13px;
}

@media (max-width: 768px) {
  .form-options {
    flex-direction: column;
    gap: 12px;
  }
  
  .form-actions,
  .result-actions {
    flex-direction: column;
  }
  
  .btn-primary,
  .btn-secondary {
    width: 100%;
  }
}
</style>